package it.yuzuojian.com.controller;

import com.github.pagehelper.PageInfo;
import it.yuzuojian.com.javaBean.Type;
import it.yuzuojian.com.service.TypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin/type")
public class TypeController {
    @Autowired
    private TypeService typeService;
    @GetMapping("/findAll")
    @RequiresPermissions("type_select")
    public String findAll(Model model,
                          @RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "pageSize", defaultValue = "2") int pageSize){
        List<Type> types = typeService.findAll(page,pageSize);
        PageInfo<Type> pageInfo=new PageInfo<>(types);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo",pageInfo);
        if(model.getAttribute("msg")==null){
            model.addAttribute("msg",null);
        }
        return "admin/types";

    }
    @GetMapping("/add/index")
    @RequiresPermissions("type_add")
    public String addIndex(Model model){

        model.addAttribute("type",new Type());
        return "admin/types-input";
    }
    @Transactional
    @PostMapping("/add")
    @RequiresPermissions("type_add")
    public String add(Type type, Model model, RedirectAttributes attributes){
        type.setCount(0);
        try {
            typeService.add(type);
            attributes.addFlashAttribute("msg","添加完成");
        }catch (Exception e){
            attributes.addFlashAttribute("msg","添加失败");
            return "redirect:/admin/type/findAll";
        }
        model.addAttribute("type",type);
        return "redirect:/admin/type/findAll";
    }
    @Transactional
    @GetMapping("/{id}/delete")
    @RequiresPermissions("type_del")
    public String delete(RedirectAttributes attributes,@PathVariable("id") Integer id){
        try {
            typeService.deleteArticleTypeName(id);
            typeService.deleteById(id);
            attributes.addFlashAttribute("msg","删除完成");
        }catch (Exception e){
            attributes.addFlashAttribute("msg","删除失败");
            return "redirect:/admin/type/findAll";
        }
        return "redirect:/admin/type/findAll";
    }
    @GetMapping("/{id}/update/index")
    @RequiresPermissions("type_update")
    public String updateIndex(@PathVariable("id")Integer id,Model model){
        Type type = typeService.findById(id);
        model.addAttribute("type",type);
        return "admin/types-input";
    }
    @Transactional
    @PostMapping("/{id}/update")
    @RequiresPermissions("type_update")
    public String update(@PathVariable("id")Integer id, Type type,RedirectAttributes attributes){
        try {
            typeService.update(type);
            attributes.addFlashAttribute("msg","操作成功");
        }catch (Exception e){
            attributes.addFlashAttribute("msg","操作失败");
        }

        return "redirect:/admin/type/findAll";
    }

}
